package com.jtoon.servlet;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jtoon.bean.Comics;
import com.jtoon.bean.Img;
import com.jtoon.util.PMF;

public class Index extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Comics.class);
		
		HttpSession session = request.getSession();
		if(session.getAttribute("id") != null){
			request.setAttribute("isLogin", true);
		}
		
		try{ 
			List<Comics> comicsList = (List<Comics>)query.execute();
			
			request.setAttribute("comicsList", comicsList);
			 
			RequestDispatcher rd = request.getRequestDispatcher("/page/index.jsp"); 
			rd.forward(request, response);  
			
		}finally{
			query.closeAll(); 
			pm.close(); 
		}
		
	}
	
}
